package com.mulidatasource.demo.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 多数据源配置
 */
@Configuration
public class DatasourceConfiguration {

    /**
     * 主数据源，mysql
     * @return datasource
     */
    @Bean("mysqlDatasource")
    @Primary
    @ConfigurationProperties("spring.datasource.hikari.mysql") // 默认是配置的前缀
    public DataSource mysqlDatasource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 次数据源
     * @return postgresql
     */
    @Bean("postgresDatasource")
    @ConfigurationProperties("spring.datasource.hikari.postgres")
    public DataSource postgresDataSource() {
        return DataSourceBuilder.create().build();
    }
}
